package com.bwsoft.util;

import org.apache.log4j.Logger;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.APIConnectionException;
import cn.jpush.api.common.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.report.ReceivedsResult;

import com.bwsoft.api.service.impl.QualityControlServiceImpl;

public class JPushUtil {

	private static Logger LOG = Logger
			.getLogger(QualityControlServiceImpl.class);
	
	private static final String masterSecret="11df248d395bcde6f5d741ba";
	private static final String appKey="3ea7d34f43d254a3d485ec99";
	
	public static void simplePushMessage( String alias,String msgContent) {

		   JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);

	        // For push, all you need do is to build PushPayload object.
	        PushPayload payload = buildPushObject_all_alias_alert(alias, msgContent);

	        try {
	            PushResult result = jpushClient.sendPush(payload);
	            LOG.info("Got result - " + result);

	        } catch (APIConnectionException e) {
	            // Connection error, should retry later
	            LOG.error("Connection error, should retry later", e);

	        } catch (APIRequestException e) {
	            // Should review the error, and fix the request
	            LOG.error("Should review the error, and fix the request", e);
	            LOG.info("HTTP Status: " + e.getStatus());
	            LOG.info("Error Code: " + e.getErrorCode());
	            LOG.info("Error Message: " + e.getErrorMessage());
	        }
	}
	
	
	public static void getResult(String id){
		JPushClient jpushClient = new JPushClient(masterSecret, appKey);
	    try {
	        ReceivedsResult result = jpushClient.getReportReceiveds(id);
	        LOG.info("Got result - " + result);

	    } catch (APIConnectionException e) {
	        // Connection error, should retry later
	        LOG.error("Connection error, should retry later", e);

	    } catch (APIRequestException e) {
	        // Should review the error, and fix the request
	        LOG.error("Should review the error, and fix the request", e);
	        LOG.info("HTTP Status: " + e.getStatus());
	        LOG.info("Error Code: " + e.getErrorCode());
	        LOG.info("Error Message: " + e.getErrorMessage());
	    }
	}
	
	
	//构建推送对象：平台是 Andorid 与 iOS，推送目标是 （"tag1" 与 "tag2" 的并集）且（"alias1" 与 "alias2" 的并集），推送内容是 - 内容为 MSG_CONTENT 的消息，并且附加字段 from = JPush。
	public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras(String alias,String message) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.alias(alias))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(message)
                        .addExtra("from", "JPush")
                        .build())
                .build();
    }
	
	//构建推送对象：所有平台，推送目标是别名为 "alias1"，通知内容为 ALERT。
	 public static PushPayload buildPushObject_all_alias_alert(String alias,String message) {
	        return PushPayload.newBuilder()
	                .setPlatform(Platform.all())
	                .setAudience(Audience.alias(alias))
	                .setNotification(Notification.alert(message))
	                .build();
	    }
	 
	 //快捷地构建推送对象：所有平台，所有设备，内容为 ALERT 的通知。
	 public static PushPayload buildPushObject_all_all_alert(String message) {
	        return PushPayload.alertAll(message);
	    }
	 

}
